<!DOCTYPE html>
<html
        layout:decorator="component/mainLayout"
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:th="http://www.thymeleaf.org"
        lang="en" xmlns:layout="http://www.w3.org/1999/xhtml">
<head>
    <link rel="stylesheet" type="text/css" href="/layui/css/layui.css" media="all"/>
    <script src="/layui/layui.js" charset="utf-8"></script>
</head>
<body layout:fragment="contentCenter">

<div class="layui-fluid">
    <div class="layui-card" style="    border: 1px solid darkgray;">
        <div class="layui-card-header" style="font-size: 18px">货物信息</div>
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="component-form-group">
                <input type="hidden" id="cargoUrl" name="cargoUrl">
                <div class="layui-form-item">
                    <label class="layui-form-label">货物名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="cargoName" lay-verify="required" autocomplete="off" placeholder="请输入货物名称" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">商品数量</label>
                    <div class="layui-input-block">
                        <input type="text" name="cargoNum" lay-verify="required|number" autocomplete="off" placeholder="请输入商品数量" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">货物原价</label>
                        <div class="layui-input-inline" style="width: 100px;">
                            <input type="text" name="cargoPrice" placeholder="￥" lay-verify="required|number" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">零售价格</label>
                        <div class="layui-input-inline" style="width: 100px;">
                            <input type="text" name="cargoRetailPrice" placeholder="￥" lay-verify="required|number" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">过期日期</label>
                        <div class="layui-input-inline">
                            <input type="text" name="expireTime" id="LAY-component-form-group-date" lay-verify="date"
                                   placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="test1">
                          <i class="layui-icon">&#xe67c;</i>商品图片
                    </button>
                        <img class="layui-upload-img" id="demo1" style="width: 160px;margin-left: 100px" >
                </div>

                <!--<div class="layui-form-item">-->
                    <!--<div class="layui-inline">-->
                        <!--<label class="layui-form-label">多规则验证</label>-->
                        <!--<div class="layui-input-inline">-->
                            <!--<input type="text" name="number" lay-verify="required|number" autocomplete="off" class="layui-input">-->
                        <!--</div>-->
                    <!--</div>-->
                    <!--<div class="layui-inline">-->
                        <!--<label class="layui-form-label">验证日期</label>-->
                        <!--<div class="layui-input-inline">-->
                            <!--<input type="text" name="date" id="LAY-component-form-group-date" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">-->
                        <!--</div>-->
                    <!--</div>-->
                <!--</div>-->

                <!--<div class="layui-form-item">-->
                    <!--<label class="layui-form-label">验证身份证</label>-->
                    <!--<div class="layui-input-block">-->
                        <!--<input type="text" name="identity" lay-verify="identity" placeholder="" autocomplete="off" class="layui-input">-->
                    <!--</div>-->
                <!--</div>-->
                <!--<div class="layui-form-item">-->
                    <!--<label class="layui-form-label">自定义验证</label>-->
                    <!--<div class="layui-input-inline">-->
                        <!--<input type="password" name="password" lay-verify="pass" placeholder="请输入密码" autocomplete="off" class="layui-input">-->
                    <!--</div>-->
                    <!--<div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>-->
                <!--</div>-->

                <!--<div class="layui-form-item layui-layout-admin">-->
                    <!--<div class="layui-input-block">-->
                        <!--<div class="layui-btn-container">-->
                            <!--<button class="layui-btn" lay-submit="" lay-filter="component-form-demo1">立即提交</button>-->
                            <!--<button type="reset" class="layui-btn layui-btn-primary">重置</button>-->
                        <!--</div>-->
                    <!--</div>-->
                <!--</div>-->

                <div class="layui-form-item" style="margin-top: 30px">
                    <div class="layui-input-inline" style="float: right">
                        <button class="layui-btn" lay-submit lay-filter="component-form-demo1">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>


            </form>
        </div>
    </div>
</div>


<script>
    $(document).ready(function () {
        sliderCollapse.open('#cargoCollapse');
    })


    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['form', 'laydate','upload','element'], function(){
        var $ = layui.$
            ,upload = layui.upload
            ,admin = layui.admin
            ,element = layui.element
            ,layer = layui.layer
            ,laydate = layui.laydate
            ,form = layui.form;

        form.render(null, 'component-form-group');

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: '/cargoManager/uploadImg'
            // ,accept:'images'     //默认允许上传图片
            // ,auto: false
            ,field:'imgFile'    //设置后台接收参数得FileName  layui默认name='file'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                console.log(res);
                //如果上传失败
                if(res.status == 200){
                    $("#cargoUrl").val(res.data);
                }else{
                    return layer.msg('上传失败');
                }
                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });


        laydate.render({
            elem: '#LAY-component-form-group-date'
        });

        /* 自定义验证规则 */
        form.verify({
            title: function(value){
                if(value.length < 5){
                    return '标题至少得5个字符啊';
                }
            }
            ,pass: [/(.+){6,12}$/, '密码必须6到12位']
            ,content: function(value){
                layedit.sync(editIndex);
            }
        });

        /* 监听指定开关 */
        form.on('switch(component-form-switchTest)', function(data){
            layer.msg('开关checked：'+ (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        });

        /* 监听提交 */
        form.on('submit(component-form-demo1)', function(data){
            var cargoForm = {
                cargoUrl:data.field.cargoUrl,
                cargoName:data.field.cargoName,
                cargoNum:data.field.cargoNum,
                cargoPrice:data.field.cargoPrice,
                cargoRetailPrice:data.field.cargoRetailPrice,
                expireTime:data.field.expireTime,
            }
            $.ajax({
                type:'post',
                url:'/cargoManager/saveCargo',
                data:cargoForm,
                success:function(res){
                    if(res.status == 200){
                        layer.alert("上传商品信息成功！",{icon:1});
                    }
                },
                error:function (res) {
                    layer.alert("上传商品信息失败！",{icon:0});
                }
            });
            // parent.layer.alert(JSON.stringify(data.field), {
            //     title: '最终的提交信息'
            // })
            return false;
        });
    });
</script>
<style>
    .layui-form-label{
        width: 100px;
    }
    .layui-input-block{
        width:400px;
    }

</style>
</body>
</html>